const envMap = new Map([
  ["dev", "VITE_APP_DEV"],
  ["hub", "VITE_APP_HUB"],
  ["enableMqtt", "VITE_APP_ENABLEMQTT"],
  ["mqttUrl", "VITE_APP_MQTTURL"],
  ["anonymousUser", "VITE_APP_ANONYMOUSUSER"],
  ["anonymousPwd", "VITE_APP_ANONYMOUSPWD"],
  ["enableAnonymous", "VITE_APP_ENABLEANONYMOUS"],
  ["logLevel", "VITE_APP_LOGLEVEL"],
  ["baseUrl", "VITE_APP_BASEURL"],
  ["appId", "VITE_APP_APPID"],
  ["pluginBaseUrl", "VITE_APP_PLUGINBASEURL"],
  ["isLocalModel", "VITE_APP_ISLOCALMODEL"],
  ["remoteModelUrl", "VITE_APP_REMOTEMODELURL"],
  ["assetsUrl", "VITE_APP_ASSETSURL"],
  ["dcSystem", "VITE_APP_MOCKDCSYSTEMID"],
  ["downloadFileUrl", "VITE_APP_DOWNLOADFILEURL"],
  ["uploadFileUrl", "VITE_APP_UPLOADFIELURL"],
  ["casLoginUrl", "VITE_APP_CASLOGINURL"],
  ["loginMode", "VITE_APP_LOGINMODE"],
  ["menuPermissionMode", "VITE_APP_MENUPERMISSIONMODE"],
  ["enablePermission", "VITE_APP_ENABLEPERMISSION"],
  ["routePlaceholder", "VITE_APP_ROUTEPLACEHOLDER"],
  ["enableWfAllHistory", "VITE_APP_ENABLEWFALLHISTORY"],
  ["loginMode", "VITE_APP_LOGINMODE"],
  ["loginMode", "VITE_APP_LOGINMODE"],
  ["isMob", "VITE_APP_ISMOB"],
  ["isSaaSMode", "VITE_APP_ISSAAMODE"],
  ["AppTitle", "VITE_APP_APPTIELE"],
  ["favicon", "VITE_APP_FAVICON"],
  ["enableTitle", "VITE_APP_ENABLETITLE"],
]);

/**
 * 是否为boolean值字符串
 *
 * @author tony001
 * @date 2024-09-23 18:09:08
 * @export
 * @param {string} str
 * @return {*}
 */
function isBooleanString(str: string) {
  return ["true", "false", "1", "0"].indexOf(str) !== -1;
}

/**
 * 挂载环境变量
 *
 * @author tony001
 * @date 2024-09-23 18:09:50
 * @export
 * @param {IData} env
 * @return {*}  {Promise<void>}
 */
export async function attachEnvironmentConfig(): Promise<void> {
  const env: IData = import.meta.env;
  // 当前环境的值覆盖到ibiz.env里
  Object.keys(ibiz.env).forEach((key) => {
    const key2 = envMap.has(key) ? envMap.get(key)! : key;
    if (env[key2] != null) {
      (ibiz.env as any)[key] = isBooleanString(env[key2])
        ? Boolean(env[key2])
        : env[key2];
    }
  });
  // 将移动端搭载平台加入ibiz.env里
  ibiz.env.mobPlatform = __UNI_PLATFORM__;
  ibiz.log.setLevel(ibiz.env.logLevel);
}
